package com.yun.algorithmproblem.leetcode;

import java.util.HashMap;

public class Leetcode560 {

    public static void main(String[] args) {
        Leetcode560 obj = new Leetcode560();
        System.out.println(obj.subarraySum(new int[]{1, 1, 1}, 2));
    }

    public int subarraySum(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(0, 1);
        int sum = 0;
        int res = 0;
        for (int num : nums) {
            sum += num;
            res += map.getOrDefault(sum - k, 0);
            map.compute(sum, (key, v) -> v == null ? 1 : v + 1);
        }
        return res;
    }
}
